package tut.mysql.load;

import java.util.ArrayList;
import java.util.HashMap;

import tut.mysql.db.Db;
import tut.mysql.load.conf.ConfTest;
import tut.mysql.load.loadThread.ClickThread;
import tut.mysql.load.loadThread.DeliverThread;

public class Test {

	public static void main(String args[]) {
		try {
			start();
		} catch (Exception e) {
			System.out.println("main + " + e.toString());
		}
	}

	public static void start() throws Exception {

		ConfTest conf = new ConfTest();

		ArrayList<Thread> list1 = getThread(conf.getConfMap(), true);

		ArrayList<Thread> list2 = getThread(conf.getConfMap(), false);

		runThread(list1);
		runThread(list2);
	}

	public static ArrayList<Thread> getThread(HashMap<String, String> map, boolean isClickThread) {

		int max = isClickThread ? Integer.parseInt(map.get(ConfTest.CLICK_THREADS)) : Integer.parseInt(map.get(ConfTest.DELIVER_THREADS));
		int queries =  isClickThread ? Integer.parseInt(map.get(ConfTest.CLICK_QUERIES)) : Integer.parseInt(map.get(ConfTest.DELIVER_QUERIES));


		ArrayList<Thread> list = new ArrayList<Thread>();
		for (int i = 0; i < max; i++) {
			Db db = new Db(map.get(ConfTest.DB_SERVER), map.get(ConfTest.USER), map.get(ConfTest.PASSWORD), map.get(ConfTest.DB_NAME));
			list.add(new Thread(isClickThread ? new ClickThread(queries, db) : new DeliverThread(queries, db)));
		}
		return list;
	}

	public static void runThread(ArrayList<Thread> list) {
		int size = list.size();
		for(int i=0; i<size; i++) {
			Thread t = list.get(i);
			t.start();
		}
	}

}
